package com.gitee.ywj1352.zk.quickStart;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.*;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

/**
 * @author wenjie.yang on 2019/6/19.
 */
public class CuratorDemo {

    public static void main(String[] args) throws IOException {
        // crud();
        addListenerWithPathChildCache(getCuratorFramework(),"/ywj");
        System.in.read();

     }

    private static void crud() {
        CuratorFramework curator = getCuratorFramework();
        try {
            //原生api 的时候必须 逐层创建  curator 可以
            curator.create().creatingParentsIfNeeded()
                    .withMode(CreateMode.PERSISTENT).forPath("/ywj/node1","1".getBytes());
            //curator.delete().deletingChildrenIfNeeded().forPath("/ywj/node1");
            Stat stat = new Stat();
            curator.getData().storingStatIn(stat).forPath("/ywj/node1");

            curator.setData().withVersion(stat.getVersion()).forPath("/ywj/node1","xxxxx".getBytes());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static CuratorFramework getCuratorFramework() {
        CuratorFramework curator = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183").sessionTimeoutMs(4000)
                .retryPolicy(new ExponentialBackoffRetry(100, 3)).namespace("curator").build();
        curator.start();
        return curator;
    }

    //pathChildCache 监听一个子节点的 创建 删除 更新
    public static void addListenerWithPathChildCache(CuratorFramework curatorFramework,String path){
        PathChildrenCache pathChildCache = new PathChildrenCache(curatorFramework,"/ywj",false);
        PathChildrenCacheListener listener = (client,event)->{
            System.out.println("Receive Event" + event.getType());
        };
        pathChildCache.getListenable().addListener(listener);
        try {
            //启动子节点监听
            pathChildCache.start(PathChildrenCache.StartMode.NORMAL);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //nodeCache 监听一个节点的的更新和创建事件
    public static void addListenerWithNodeCache(CuratorFramework curatorFramework,String path){
        NodeCache nodeCache = new NodeCache(curatorFramework,"/ywj",false);
        NodeCacheListener listener = ()->{
            System.out.println("Receive Event" + nodeCache.getCurrentData().getPath());
        };
        nodeCache.getListenable().addListener(listener);
        try {
            nodeCache.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //treeCache 综合pathChildCache和nodeCache的特性   综合节点监听事件
    public static void addListenerWithTreeCache(CuratorFramework curatorFramework,String path){
        TreeCache treeCache = new TreeCache(curatorFramework,"/ywj");
        TreeCacheListener listener = (client,event)->{
            System.out.println("Receive Event" + event.getType());
        };
        treeCache.getListenable().addListener(listener);
        try {
            treeCache.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
